// 2025/5/19
// 滑雪

#include <iostream>
#include <vector>
using namespace std;

vector<pair<int, int>> moving = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void dfs(vector<vector<int>>& matrix, vector<vector<int>>& lens, int i, int j)
{
    int tmp = 0;
    for(int k = 0; k < 4; k++)
    {
        int x = i + moving[k].first, y = j + moving[k].second;
        if(matrix[x][y] > matrix[i][j])
        {
            if(lens[x][y] == 0)
                dfs(matrix, lens, x, y);
            tmp = max(tmp, lens[x][y]);
        }
    }
    lens[i][j] = tmp + 1;
}

int main() {
    int m, n; cin >> m >> n;
    vector<vector<int>> matrix(m + 2, vector<int>(n + 2));
    vector<vector<int>> lens(m + 2, vector<int>(n + 2, 0));
    for(int i = 1; i <= m; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            cin >> matrix[i][j];
        }
    }

    int ans = 0;
    for(int i = 1; i <= m; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(lens[i][j] == 0)
                dfs(matrix, lens, i, j);
            ans = max(ans, lens[i][j]);
        }
    }
    cout << ans << endl;
}
// 64 位输出请用 printf("%lld")